package cn.zh.org.controller;

import cn.zh.base.annotation.PreAuthorize;
import cn.zh.base.util.AjaxResult;
import cn.zh.base.util.PageList;
import cn.zh.org.domain.Department;
import cn.zh.org.query.DepartmentQuery;
import cn.zh.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dept")
@RestController
public class DepartmentController {
    @Autowired
    private DepartmentService service;

//    @GetMapping("/{id}")
//    public Department findById(Long id) {
//        return service.findById(id);
//    }

    @GetMapping
    public List<Department> findAll() {
        return service.findAll();
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        if (null == department.getId()) {
            service.add(department);
        } else {
            service.update(department);
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        service.delete(id);
        return AjaxResult.me().success();
    }

    @PatchMapping("/batchdel")
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        System.out.println(ids);
        service.batchDel(ids);
        return AjaxResult.me().success();
    }

    @PostMapping
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query) {
        return service.queryByPage(query);
    }

    @GetMapping("/tree")
    @PreAuthorize(name = "部门树",sn = "department:tree")
    public List<Department> tree() {
        return service.tree();
    }

}
